﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.Common.BLL;
using Yipusoft.Exercitation.Model;
using Yipusoft.Exercitation.BLL;

public partial class Combat_InputList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitData();
            Bind();
        }
    }
    private void InitData()
    {
        for (int i = 2010; i <= 2050; i++)
        {
            ddlYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
        }
        for (int i = 1; i <= 12; i++)
        {
            ddlMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }

        ddlYear.SelectedValue = string.IsNullOrEmpty(Request["Year"]) ? DateTime.Now.Year.ToString() : Request["Year"];
        ddlMonth.SelectedValue = string.IsNullOrEmpty(Request["Month"]) ? DateTime.Now.Month.ToString() : Request["Month"];

        ddlStation.DataSource = CodeItem.GetList(" ParentID='SBGW0000'");
        ddlStation.DataTextField = "itemName";
        ddlStation.DataValueField = "ItemID";
        ddlStation.DataBind();
        ddlStation.Items.Insert(0, new ListItem("全部", "0000"));

        ddlStation.SelectedValue = string.IsNullOrEmpty(Request["StationID"]) ? "0000" : Request["StationID"];

        ddlRank.DataSource = CodeItem.GetList(" ParentID='ZXSBJX00'");
        ddlRank.DataTextField = "ItemName";
        ddlRank.DataValueField = "ItemID";
        ddlRank.DataBind();
        ddlRank.Items.Insert(0, new ListItem("全部", "0000"));

        ddlRank.SelectedValue = string.IsNullOrEmpty(Request["RankID"]) ? "0000" : Request["RankID"];
        string departmentID = string.IsNullOrEmpty(Request["DepartmentID"]) ? Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID : Request["DepartmentID"];
        ddlDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList(" Left(DepartmentID,4)='" + departmentID.Substring(0,4)+ "' And DepartmentID<>'1300000'");
        ddlDepartment.DataTextField = "ShortName";
        ddlDepartment.DataValueField = "DepartmentID";
        ddlDepartment.DataBind();
        
        ddlDepartment.SelectedValue = departmentID;
    }

    public string GetTaskID()
    {
        TaskBaseInfo taskInfo = TaskBase.GetList(" Year(EndDate)=" + ddlYear.SelectedValue + " And Month(EndDate)=" + ddlMonth.SelectedValue + "").FirstOrDefault();
        return taskInfo != null ? taskInfo.TaskID.ToString() : Guid.Empty.ToString();
    }
    public string GetRankAndStation()
    {
        string where = string.Empty;
        if (!ddlRank.SelectedValue.Equals("0000"))
        {
            where = " And RankID='" + ddlRank.SelectedValue + "'";
        }
        if (!ddlStation.SelectedValue.Equals("0000"))
        {
            where += " And StationID='" + ddlStation.SelectedValue + "'";
        }

        return where;
    }

    private void Bind()
    {
        string where = GetRankAndStation();
        string departmentID = ddlDepartment.SelectedValue;
       
      
        //已录且已达标人
        List<TaskSoldierScoreInfo> soldierList = TaskSoldierScore.GetList(" TaskID='" + GetTaskID() + "' And DepartmentID='" + departmentID + "' And SoldierID IN (Select SoldierID From TaskSoldiers Where DepartmentID='" + departmentID + "'" + where + " And TaskID='" + GetTaskID() + "') And Score=1");
       dltSoldiers.DataSource = soldierList;
       dltSoldiers.DataBind();

       //已录入未达标人
       List<TaskSoldierScoreInfo> soldierList2 = TaskSoldierScore.GetList("  TaskID='" + GetTaskID() + "' And DepartmentID='" + departmentID + "' And SoldierID IN (Select SoldierID From TaskSoldiers Where DepartmentID='" + departmentID + "'" + where + " And TaskID='" + GetTaskID() + "') And Score<1");
       dltSoldiers2.DataSource = soldierList2;
       dltSoldiers2.DataBind();


    }
    public string GetSoldierName(object soldierID)
    {
        if (soldierID == null)
            return "";

        Yipusoft.SoldierArchives.BLL.Soldier soldier = new Soldier();
        return soldier.GetSoldierInfoByID(soldierID.ToString()).SoldierName;
    }
    public string GetRankID(string soldierID)
    {
        TaskSoldiersInfo soldierInfo = TaskSoldiers.GetTaskSoldiers(soldierID);
        return soldierInfo != null ? soldierInfo.RankID : Guid.Empty.ToString();
    }
    protected void lnbtnSoldier_Command(object sender, CommandEventArgs e)
    {
        //Bind();
    }
    protected void dltSoldiers_ItemDataBound(object sender, DataListItemEventArgs e)
    {

    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        Bind();
    }
}
